package com.zoweunion.mechanic.controller;

import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.entity.TSBrOrderDetailFaultEntity;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.ITSBrOrderDetailFaultService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * ${comments}
 *
 * @author wxx
 * @date 2023-04-28
 */
@RestController
@RequestMapping(path = "/tSBrOrderDetailFault", produces = "application/json;charset=UTF-8")
// @Api(value = "/tSBrOrderDetailFault", description = "${comments}", produces = "application/json;charset=UTF-8")

public class TSBrOrderDetailFaultController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(TSBrOrderDetailFaultController.class);

    @Autowired
    private ITSBrOrderDetailFaultService tSBrOrderDetailFaultService;

    @GetMapping("/listPage")
//     @ApiOperation(value = "条件查询列表分页", notes = "条件查询列表分页")
    public ResponseBean listPage(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {

        params.put("user_id", getCurrentUser(request).getId());
        PageInfo info = tSBrOrderDetailFaultService.listPage(params);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }


    /**
     * 新增
     */
    @PostMapping
//    @ApiOperation(value = "新增", notes = "")
    public ResponseBean create(@RequestBody @Validated TSBrOrderDetailFaultEntity tSBrOrderDetailFault, HttpServletRequest request) {

        try {
            tSBrOrderDetailFaultService.save(tSBrOrderDetailFault);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } catch (Exception e) {
            return new ResponseBean(Constants.CODE_ERROR, e.getMessage(), "");
        }

    }


    /**
     * 更新
     */
    @PutMapping
//    @ApiOperation(value = "更新", notes = "")
    public ResponseBean update(@RequestBody TSBrOrderDetailFaultEntity tSBrOrderDetailFault, HttpServletRequest request) {

        if (tSBrOrderDetailFault.getId() != null) {
            tSBrOrderDetailFaultService.updateById(tSBrOrderDetailFault);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } else {
            return new ResponseBean(Constants.CODE_ERROR, "id必填", "");
        }
    }


    /**
     * 详情
     */
    @GetMapping("/{id}")
//    @ApiOperation(value = "详情", notes = "")
    public ResponseBean detail(@PathVariable("id") String id) {
        try {

            TSBrOrderDetailFaultEntity tSBrOrderDetailFault = tSBrOrderDetailFaultService.getById(id);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", tSBrOrderDetailFault);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseBean(Constants.CODE_ERROR, "操作失败", "");
        }

    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
//    @ApiOperation(value = "删除", notes = "")
    public ResponseBean delete(@RequestBody Long[] ids) {
        try {

            tSBrOrderDetailFaultService.removeByIds(Arrays.asList(ids));
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseBean(Constants.CODE_ERROR, "操作成功", "");
        }
    }



}
